package ext.acae2.cad;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.log4j.Logger;

import wt.content.ApplicationData;
import wt.content.ContentHelper;
import wt.content.ContentItem;
import wt.content.ContentRoleType;
import wt.content.Streamed;
import wt.epm.EPMDocument;
import wt.fc.ObjectReference;
import wt.fc.PersistenceHelper;
import wt.fc.PersistenceServerHelper;
import wt.fc.QueryResult;
import wt.log4j.LogR;
import wt.representation.Representable;
import wt.representation.Representation;
import wt.representation.RepresentationHelper;

/**
 * @desription 可视化PDF的替换
 * @author cnie
 * @date 2013-7-29 下午5:44:26
 */
public class RepresentationReplace {
	
	private static final Logger LOGGER = LogR.getLogger(RepresentationReplace.class.getName());
	
	/**
	 * 
	 * @description 可视化PDF的替换，调用此方法需要在加上事务
	 * @author cnie
	 * @date 2013-8-9 下午3:27:54
	 * @version 1.0
	 * @param oid
	 * @param filePath
	 * @throws Exception
	 */
	public static boolean replaceRepresentationPdf(EPMDocument epmdocument,File replacePdfFile) throws Exception{
		
		InputStream is = null;
//		Transaction trans = null;
		//File replacePdfFile = new File(filePath);
		try{
//			RevisionControlled rc = (RevisionControlled) CSCUtil.getObjectByOid(oid);
			Representation representation = RepresentationHelper.service.getDefaultRepresentation((Representable) epmdocument);
			if(representation==null){
				LOGGER.debug(epmdocument.getName()+">>>>>>>>>>>>>>>>>>>>>>>>>>>没有关联可视化");
				return false;
			}
			
			if(replacePdfFile==null||!replacePdfFile.exists()){
				LOGGER.debug("替换的文件不存在或者不是PDF文件");
				return false;
			}
			
			LOGGER.info("<<<<<<<<<<<<<<<<<start replace>>>>>>>>>>>>>>>>");
			is = new FileInputStream(replacePdfFile);
			long fileSize= replacePdfFile.length();
			LOGGER.info("<<<<<<<<<<<<<<<<<fileSize>>>>>>>>>>>>>>>>"+fileSize);
			System.out.println(fileSize);
			QueryResult contentQr = ContentHelper.service.getContentsByRole(representation, ContentRoleType.SECONDARY);
//			trans = new Transaction();
//       	    trans.start();
			while(contentQr.hasMoreElements()){
				ContentItem contentItem = (ContentItem) contentQr.nextElement();
				if(!(contentItem instanceof ApplicationData))
					continue;
				 ApplicationData appData = (ApplicationData) contentItem;
				 String appDataFileName = appData.getFileName();
				 if(!appDataFileName.toUpperCase().endsWith(".PDF"))
					 continue;
				 Streamed streamed = null;
            	 ObjectReference or = appData.getStreamData();
            	 if(or.isObjectInflated())
            		 streamed = (Streamed) or.getObject();
            	 else
            		 streamed = (Streamed)PersistenceHelper.manager.refresh(or.getObjectId());
            	 if(streamed==null){
            		 LOGGER.info(appDataFileName+">>>>>>>>>获取不到关联的StreamData");
            		 continue;
            	 }
            	 
            	 PersistenceHelper.manager.lockAndRefresh(streamed);
        		 streamed.storeStream(representation, appData, is);
        		 
        		 appData.setFileSize(fileSize);
        		 PersistenceServerHelper.manager.update(appData);
        		 PersistenceHelper.manager.refresh(appData);
//        		 trans.commit();
			}
			LOGGER.info("<<<<<<<<<<<<<<<<<end replace>>>>>>>>>>>>>>>>");
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}finally{
//			if(trans!=null)
//				trans=null;
			if(is!=null)
				is.close();
//			if(replacePdfFile.exists())
//				replacePdfFile.delete();
		}
		return true;
	}

}
